/**
 * @version		$Id: jce.js 131 2011-04-01 16:26:21Z happy_noodle_boy $
 * @package		JCE
 * @copyright	Copyright (C) 2009 Ryan Demmer. All rights reserved.
 * @license		GNU/GPL
 * This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
(function($) {
    $.support.placeholder = 'placeholder' in document.createElement('input');
	
	$.jce = {

        options : {},

        init : function(options) {
            var self = this;
        	$.extend(this.options, options);
        	
        	// add ui-jce class to body
        	$('body').addClass('ui-jce');

            // Tips
            $('.hasTip').tips({
                parent : '#jce'
            });

            // Style stuff
            $('div.icon a').addClass('ui-widget-content ui-corner-all');

            $('a.dialog').click(function(e) {
            	self.createDialog({
            		src 	: $(this).attr('href'),
            		options : $(this).data('options'),
            		modal	: $(this).hasClass('modal'),
            		type	: /(users|help|preferences|updates|browser|legend)/.exec($(this).attr('class'))[0],
            		title	: $(this).attr('title')
            	});
            	e.preventDefault();
            });

            // IE
            if (!$.support.cssFloat) {
                // IE6
                if (!window.XMLHttpRequest) {
                    $('input:text').addClass('ie_input_text');

                    $('ul.adminformlist > li, dl.adminformlist > dd').addClass('ie_adminformlist');
                    $('ul.adminformlist > li > label:first-child, ul.adminformlist > li > span:first-child, dl.adminformlist > dd > label:first-child, dl.adminformlist > dd > span:first-child').addClass('ie_adminformlist_child');
                }
                // IE6 / IE7
                if (!document.querySelector) {
                    $('button').addClass('ie_button');
                }
            }

            // Profiles list
            // buttons
            $('button#filter_go').button({
                icons: {
                    primary: 'ui-icon-search'
                }
            });

            $('button#filter_reset').button({
                icons : {
                    primary : 'ui-icon-arrowrefresh-1-e'
                }
            });

            // Table striping
            $('div#jce tbody tr:odd').addClass('odd');
            
            // HTML5 style form elements
            self.formWidgets();        
        },

        createDialog : function(o) {
            var self = this;

            function _fixDialog(el, settings) {
                // opera bug?
                if (parseFloat(el.style.height) == 0) {
                    var h = settings.height;
                    // get height of siblings
                    $(el).siblings('div').each( function() {
                        h = h - parseFloat($(this).outerHeight());
                    });

                    // remove border, padding etc.
                    h = h - $(el).outerHeight();
                    // set height and centre
                    $(el).css('height', h).dialog('option', 'position', 'center');
                }
            }

            var buttons = {};
            var div		= document.createElement('div');
            var loader	= document.createElement('div');
            var title 	= o.title || '';

            var iframe 	= document.createElement('iframe');

            if (o.type == 'users') {
                $.extend(buttons, {
                    '$select' : function() {
                        iframe.contentWindow.selectUsers();
                        $(this).dialog("close");
                    }

                });
            }

            if (o.type == 'preferences') {
                $.extend(buttons, {
                    '$save' : function() {
                        iframe.contentWindow.submitform('save');
                    }

                });
            }
            
            var src = o.src, data = {};
                
            // add optional settings from link
            if ($.type(o.options) == 'string') {
            	data = $.parseJSON(o.options.replace(/'/g, '"'));
            } else {
            	data = o.options;
            }

            var settings = {
                bgiframe: true,
                width 	: 640,
                height	: 480,
                modal	: o.modal || false,
                buttons : buttons,
                resizable: true,
                open : function() {
                    _fixDialog(div, settings);
                    $(loader).addClass('loader').appendTo(div);

                    $(iframe).css({width : '100%', height : '100%'}).attr({
                        src 		: src,
                        scrolling 	: 'auto',
                        frameBorder : 'no'
                    }).appendTo(div).load( function() {
                        $(loader).hide();
                    });

                    $('button').each( function() {
                        var h = this.innerHTML;
                        h = h.replace(/\$([a-z]+)/, function(a, b) {
                            return self.options.labels[b];
                        });

                        this.innerHTML = h;
                    }).button();
                }
            };

            if (o.type == 'confirm') {
                var text 	= o.title.split('::');
                var title 	= text[0];
                var message = text.length > 1 ? text[1] : text[0];

                $.extend(settings, {
                    width 		: 300,
                    height		: 'auto',
                    resizable	: false,
                    dialogClass	: 'ui-jce',
                    buttons : {
                        '$ok' : function() {
                            if (/function\([^\)]*\)\{/.test(src)) {
                                $.globalEval(src);
                            } else {
                                document.location.href = src;
                            }
                            $(this).dialog("close");
                        },

                        '$cancel' : function() {
                            $(this).dialog("close");
                        }

                    },
                    open : function() {
                        _fixDialog(div, settings);

                        $(div).attr('id', 'dialog-confirm').append(message);

                        $('button').each( function() {
                            var h = this.innerHTML;
                            h = h.replace(/\$([a-z]+)/, function(a, b) {
                                return self.options.labels[b];
                            });

                            this.innerHTML = h;
                        }).addClass('ui-state-default ui-corner-all');
                    }
                });
            }
                
            $(div).css({
                'overflow' 	: 'hidden'
            }).attr('title', title).dialog($.extend(settings, data));
        },

        /**
         * Password input
         */
        passwordWidget : function(el) {
            var id = el.id, span = document.createElement('span');

            $(span).addClass('widget-password locked').insertAfter(el).click( function() {
                el = $(this).siblings('input[type="password"]');

                if ($(this).hasClass('locked')) {
                    var input = document.createElement('input');

                    $(el).hide();

                    $(input).attr({
                        type 	: 'text',
                        size 	: $(el).attr('size'),
                        value	: $(el).val(),
                        'class' : $(el).attr('class')
                    }).insertAfter(el).change( function() {
                        $(el).val(this.value);
                    });

                } else {
                    var n = $(this).siblings('input[type="text"]');
                    var v = $(n).val();
                    $(n).remove();
                    $(el).val(v).show();
                }
                $(this).toggleClass('locked');
            });

        },
        
        /**
         * HTML5 form widgets
         */
        formWidgets : function() {
        	var self = this;
        	
        	$('input[type="password"]').each( function() {
                self.passwordWidget(this);
            });
        	
        	// check for placeholder and create
        	if (!$.support.placeholder) {
        		// create javascript placeholder
                $('input[placeholder], textarea[placeholder]').each( function() {
                	self.addPlaceHolder(this);
                });
        	}

            // set pattern value attribute actions
            $(':input[pattern]').change(function() {
            	var pattern = $(this).attr('pattern'), v = $(this).val();
                if (!new RegExp('^(?:' + pattern + ')$').test(v)) {
                	var n = new RegExp('(' + pattern + ')').exec(v);
                	$(this).val(n[0]);
                }
            });
            
            // set max value attribute actions
            $(':input[max]').change( function() {
                var m = parseFloat($(this).attr('max')), v = parseFloat($(this).val());
                if (v > m) {
                    $(this).val(m);
                }
            });
            
            // set min value attribute actions
            $(':input[min]').change( function() {
            	var m = parseFloat($(this).attr('min')), v = parseFloat($(this).val());
                if (v < m) {
                    $(this).val(m);
                }
            });
        },
        
        /**
         * Add an HTML5 placeholder attribute or Javascript emulation of one
         * @param {Object} el Input element
         */
        addPlaceHolder : function(el) {
            var t = this, v = $(el).attr('placeholder');
            
            if (el.value == '' || el.value == v) {
                $(el).addClass('placeholder').val(v).click( function() {
                    if ($(this).hasClass('placeholder')) {
                        $(this).val('').removeClass('placeholder');
                    }
                }).blur( function() {
                    if (this.value == '' || this.value == v) {
                        $(this).addClass('placeholder').val(v);
                    }
                });
            }
            $(el).change( function() {
                if (this.value == '') {
                    $(this).addClass('placeholder').val(v);
                } else {
                    $(this).removeClass('placeholder');
                }
            });
        },
    }
})(jQuery);
// global shortcut
var $jce = jQuery.jce;
